<?php
class PHZ_LiPai
{

    public static $public_dun  = array();
    public static $public_data = array();

    /**
     * 理牌器，待强化
     * @param $pai_string
     * @param bool $is_hu
     * @param $room_info
     * @return array
     */
    public static function __lipai( $pai_string , $is_hu = false , $room_info , $hu_card = 0 , $card_info = array() ){
        //是否能在sort里就把牌处理完毕
        if( $is_hu == true && $hu_card != 0 ){
            $data = PHZ_sort::__lipaiSort( $card_info['sp'] , $card_info ,  $hu_card );
            if( $data != false ){
                return $data;
            }
        }
        //初始化数据
        self::$public_dun  = array();
        self::$public_data = explode(",", $pai_string );
        $temp_count = array_count_values( self::$public_data );
        //去除三连或者四连
        foreach ( $temp_count as $key => $value ){
            if( $value == 3 ){array_push( self::$public_dun , array($key,$key,$key) );}
            if( $value == 4 ){array_push( self::$public_dun , array($key,$key,$key,$key) );}
            if( $value == 3 || $value == 4 ){
                foreach ( self::$public_data as $key_1 => $value_1 ){
                    if( $value_1 == $key ){
                        unset( self::$public_data[$key_1] );
                    }
                }
            }
        }
        //胡牌的理牌稍微有点不同
        if( $is_hu == true ){
            //理牌大小1.2.3，大小2.7.10
            self::__number_3( 2,7,10 );self::__number_3( 12,17,20 );
            self::__number_3( 1,2,3 );self::__number_3( 11,12,13 );
            //理牌1.5.10，当然本房打的时候才理
            $mttype = $room_info['room_mtype'];
            if( strstr( $mttype  , "2" ) == true ){
                self::__number_3( 1,5,10 );self::__number_3( 11,15,20 );
            }
            //大夹小，或者小夹大的赖子情况
            self::__number_3( 1,1,11 );self::__number_3( 11,11,1 );self::__number_3( 1,11,1 );self::__number_3( 11,1,11 );self::__number_3( 11,1,1 );self::__number_3( 1,11,11 );
            self::__number_3( 2,2,12 );self::__number_3( 12,12,2 );self::__number_3( 2,12,2 );self::__number_3( 12,2,12 );self::__number_3( 12,2,2 );self::__number_3( 2,12,12 );
            self::__number_3( 3,3,13 );self::__number_3( 13,13,3 );self::__number_3( 3,13,3 );self::__number_3( 13,3,13 );self::__number_3( 13,3,3 );self::__number_3( 3,13,13 );
            self::__number_3( 4,4,14 );self::__number_3( 14,14,4 );self::__number_3( 4,14,4 );self::__number_3( 14,4,14 );self::__number_3( 14,4,4 );self::__number_3( 4,14,14 );
            self::__number_3( 5,5,15 );self::__number_3( 15,15,5 );self::__number_3( 5,15,5 );self::__number_3( 15,5,15 );self::__number_3( 15,5,5 );self::__number_3( 5,15,15 );
            self::__number_3( 6,6,16 );self::__number_3( 16,16,6 );self::__number_3( 6,16,6 );self::__number_3( 16,6,16 );self::__number_3( 16,6,6 );self::__number_3( 6,16,16 );
            self::__number_3( 7,7,17 );self::__number_3( 17,17,7 );self::__number_3( 7,17,7 );self::__number_3( 17,7,17 );self::__number_3( 17,7,7 );self::__number_3( 7,17,17 );
            self::__number_3( 8,8,18 );self::__number_3( 18,18,8 );self::__number_3( 8,18,8 );self::__number_3( 18,8,18 );self::__number_3( 18,8,8 );self::__number_3( 8,18,18 );
            self::__number_3( 9,9,19 );self::__number_3( 19,19,9 );self::__number_3( 9,19,9 );self::__number_3( 19,9,19 );self::__number_3( 19,9,9 );self::__number_3( 9,19,19 );
            self::__number_3( 10,10,20 );self::__number_3( 20,20,10 );self::__number_3( 10,20,10 );self::__number_3( 20,10,20 );self::__number_3( 20,10,10 );self::__number_3( 10,20,20 );
            //去除顺子
            self::__number_3( 2,3,4 );self::__number_3( 3,4,5 );
            self::__number_3( 4,5,6 );self::__number_3( 5,6,7 );self::__number_3( 6,7,8 );self::__number_3( 7,8,9 );
            self::__number_3( 8,9,10);self::__number_3( 11,12,13 );self::__number_3( 12,13,14 );
            self::__number_3( 13,14,15 );self::__number_3( 14,15,16 );self::__number_3( 15,16,17 );
            self::__number_3( 16,17,18 );self::__number_3( 17,18,19 );self::__number_3( 18,19,20);
            //对子去除
            for ( $i = 1 ; $i <= 20 ; $i++ ){self::__number_2( $i , $i );}
        }else {
            //理牌大小1.2.3，大小2.7.10
            self::__number_3( 2,7,10 );self::__number_3( 12,17,20 );
            self::__number_3( 1,2,3 );self::__number_3( 11,12,13 );
            //理牌1.5.10，当然本房打的时候才理
            $mttype = $room_info['room_mtype'];
            if( strstr( $mttype  , "2" ) == true ){
                self::__number_3( 1,5,10 );self::__number_3( 11,15,20 );
            }
            //去除顺子
            self::__number_3( 2,3,4 );self::__number_3( 3,4,5 );
            self::__number_3( 4,5,6 );self::__number_3( 5,6,7 );self::__number_3( 6,7,8 );self::__number_3( 7,8,9 );
            self::__number_3( 8,9,10);self::__number_3( 11,12,13 );self::__number_3( 12,13,14 );
            self::__number_3( 13,14,15 );self::__number_3( 14,15,16 );self::__number_3( 15,16,17 );
            self::__number_3( 16,17,18 );self::__number_3( 17,18,19 );self::__number_3( 18,19,20);
            //大夹小，或者小夹大的赖子情况
            self::__number_3( 1,1,11 );self::__number_3( 11,11,1 );self::__number_3( 1,11,1 );self::__number_3( 11,1,11 );self::__number_3( 11,1,1 );self::__number_3( 1,11,11 );
            self::__number_3( 2,2,12 );self::__number_3( 12,12,2 );self::__number_3( 2,12,2 );self::__number_3( 12,2,12 );self::__number_3( 12,2,2 );self::__number_3( 2,12,12 );
            self::__number_3( 3,3,13 );self::__number_3( 13,13,3 );self::__number_3( 3,13,3 );self::__number_3( 13,3,13 );self::__number_3( 13,3,3 );self::__number_3( 3,13,13 );
            self::__number_3( 4,4,14 );self::__number_3( 14,14,4 );self::__number_3( 4,14,4 );self::__number_3( 14,4,14 );self::__number_3( 14,4,4 );self::__number_3( 4,14,14 );
            self::__number_3( 5,5,15 );self::__number_3( 15,15,5 );self::__number_3( 5,15,5 );self::__number_3( 15,5,15 );self::__number_3( 15,5,5 );self::__number_3( 5,15,15 );
            self::__number_3( 6,6,16 );self::__number_3( 16,16,6 );self::__number_3( 6,16,6 );self::__number_3( 16,6,16 );self::__number_3( 16,6,6 );self::__number_3( 6,16,16 );
            self::__number_3( 7,7,17 );self::__number_3( 17,17,7 );self::__number_3( 7,17,7 );self::__number_3( 17,7,17 );self::__number_3( 17,7,7 );self::__number_3( 7,17,17 );
            self::__number_3( 8,8,18 );self::__number_3( 18,18,8 );self::__number_3( 8,18,8 );self::__number_3( 18,8,18 );self::__number_3( 18,8,8 );self::__number_3( 8,18,18 );
            self::__number_3( 9,9,19 );self::__number_3( 19,19,9 );self::__number_3( 9,19,9 );self::__number_3( 19,9,19 );self::__number_3( 19,9,9 );self::__number_3( 9,19,19 );
            self::__number_3( 10,10,20 );self::__number_3( 20,20,10 );self::__number_3( 10,20,10 );self::__number_3( 20,10,20 );self::__number_3( 20,10,10 );self::__number_3( 10,20,20 );
            //对子去除
            for ( $i = 1 ; $i <= 20 ; $i++ ){self::__number_2( $i , $i );}
            //把对子+一个大于它的字放到一起 or 反
            for ( $i = 1 ; $i <= 19 ; $i++ ){self::__number_3( $i , $i , $i+1 );}
            for ( $i = 1 ; $i <= 19 ; $i++ ){self::__number_3( $i , $i , $i-1 );}
            //1.5.10，2.7.10；单独抽离
            self::__number_2( 1,5 );self::__number_2( 5,10 );self::__number_2( 1,10 );
            self::__number_2( 2,7 );self::__number_2( 2,10 );self::__number_2( 7,10 );
            self::__number_2( 11,15 );self::__number_2( 15,20 );self::__number_2( 11,20 );
            self::__number_2( 12,17 );self::__number_2( 12,20 );self::__number_2( 17,20 );
            self::__number_2( 1,2 );self::__number_2( 2,3 );self::__number_2( 1,3 );
            self::__number_2( 11,12 );self::__number_2( 12,13 );self::__number_2( 11,13 );
            //最后去除一些奇怪的搭配
            self::__number_3( 5,6,10 );self::__number_3( 15,16,20 );self::__number_3( 2,7,8 );
            self::__number_3( 2,7,17 );self::__number_3( 12,17,7 );self::__number_3( 9,19,18 );
            self::__number_3( 6,8,16 );self::__number_3( 2,3,7 );self::__number_3( 8,10,20 );self::__number_3( 8,9,18 );
            self::__number_3( 7,17,18 );self::__number_3( 4,14,13 );self::__number_3( 3,13,12 );
            //去除大+小；或者小+大
            for ( $i = 1 ; $i <= 10 ; $i++ ){self::__number_2( ($i+10) , $i );}
            for ( $i = 1 ; $i <= 10 ; $i++ ){self::__number_2( $i , ($i+10) );}
            //去除一些伴字
            for ( $i = 1 ; $i <= 19 ; $i++ ){self::__number_2( $i , $i+1 );}
        }
        //如果还有牌没有处理完；则进行3张一蹲的强制插入
        if( count( self::$public_data ) >= 1 ){array_push( self::$public_dun , array_slice(self::$public_data,0,3) );array_splice( self::$public_data , 0 , 3 );}
        if( count( self::$public_data ) >= 1 ){array_push( self::$public_dun , array_slice(self::$public_data,0,3) );array_splice( self::$public_data , 0 , 3 );}
        if( count( self::$public_data ) >= 1 ){array_push( self::$public_dun , array_slice(self::$public_data,0,3) );array_splice( self::$public_data , 0 , 3 );}
        if( count( self::$public_data ) >= 1 ){array_push( self::$public_dun , array_slice(self::$public_data,0,3) );array_splice( self::$public_data , 0 , 3 );}
        if( count( self::$public_data ) >= 1 ){array_push( self::$public_dun , array_slice(self::$public_data,0,3) );array_splice( self::$public_data , 0 , 3 );}
        if( count( self::$public_data ) >= 1 ){array_push( self::$public_dun , array_slice(self::$public_data,0,3) );array_splice( self::$public_data , 0 , 3 );}
        //将数据进行排序操作
        foreach ( self::$public_dun as $key => $value ){
            $temp_value = $value;
            arsort( $temp_value );
            $temp_value = array_values( $temp_value );
            self::$public_dun[$key] = $temp_value;
        }
        //返回数据
        return self::$public_dun;
    }

    public static function __number_2( $n1 , $n2 ){
        $number_as = array( $n1 , $n2 );
        $all_find  = 0;
        $temp_data = self::$public_data;
        foreach ( $temp_data as $key => $value ){if( $value == $n1 ){unset( $temp_data[$key] );$all_find++;break;}}
        foreach ( $temp_data as $key => $value ){if( $value == $n2 ){unset( $temp_data[$key] );$all_find++;break;}}
        if( $all_find == 2 ){
            array_push( self::$public_dun , $number_as );
            foreach ( self::$public_data as $key => $value ){if( $value == $number_as[0] ){unset( self::$public_data[$key] );break;}}
            foreach ( self::$public_data as $key => $value ){if( $value == $number_as[1] ){unset( self::$public_data[$key] );break;}}
        }else{
            return false;
        }
    }

    public static function __number_3( $n1 , $n2 , $n3 ){
        $number_as = array( $n1 , $n2 , $n3 );
        $all_find  = 0;
        $temp_data = self::$public_data;
        foreach ( $temp_data as $key => $value ){if( $value == $n1 ){unset( $temp_data[$key] );$all_find++;break;}}
        foreach ( $temp_data as $key => $value ){if( $value == $n2 ){unset( $temp_data[$key] );$all_find++;break;}}
        foreach ( $temp_data as $key => $value ){if( $value == $n3 ){unset( $temp_data[$key] );$all_find++;break;}}
        if( $all_find >= 3 ){
            array_push( self::$public_dun , $number_as );
            foreach ( self::$public_data as $key => $value ){if( $value == $number_as[0] ){unset( self::$public_data[$key] );break;}}
            foreach ( self::$public_data as $key => $value ){if( $value == $number_as[1] ){unset( self::$public_data[$key] );break;}}
            foreach ( self::$public_data as $key => $value ){if( $value == $number_as[2] ){unset( self::$public_data[$key] );break;}}
        }else{
            return false;
        }
    }

}
?>